/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年12月25日 10:52:48
 *
 */

package com.pactera.jep.service.sys.web.controller;


import com.alibaba.fastjson.JSONObject;
import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.core.context.Context;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.service.sys.model.UserProfile;
import com.pactera.jep.sys.dto.MetaColumnDto;
import com.pactera.jep.sys.model.MetaColumn;
import com.pactera.jep.sys.service.MetaColumnService;
import com.pactera.jep.web.code.WebCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="列元数据表")
@RestController
@RequestMapping("/metaColumn")
public class MetaColumnRestController extends BaseRestController<MetaColumn> {
	private static Logger	logger	= LoggerFactory.getLogger(MetaColumnRestController.class);

	@Autowired
	private MetaColumnService	metaColumnService;


	
	/**
	 * 为列表页面做数据准备
	 */
	@Override
	protected void prepareQuery(@ApiIgnore @RequestHeader("x-user-token") String token,
			Page<MetaColumn> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {
		
		metaColumnService.query(page, filters, orders);
	}
	


	

	
	/**
	 * 保存
	 * @param metaColumn
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String, Object> save(MetaColumn metaColumn) {
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		Boolean isExist = metaColumnService.exists(metaColumn.getColumnId());
		try 
		{
			if(isExist)
			{
				
				metaColumnService.update(metaColumn);
			}
			else
			{
				metaColumnService.insert(metaColumn);
			}
			
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}
	
	/**
	 * 删除
	 * @param columnId
	 * @return
	 */
	@RequestMapping(value = "/remove/{columnId}")
	@ResponseBody
	public Map<String, Object> remove(@PathVariable String columnId){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		try 
		{
			metaColumnService.deleteByPrimaryKey(columnId);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		} 
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
		}
		
		return tips;
	}
	
	/**
	 * 验证提交的columnId是否已经存在数据库中
	 * @param columnId
	 * @return
	 */
	@RequestMapping(value = "/check", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> check(String columnId){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		boolean exist = metaColumnService.exists(columnId);
		tips.put("valid", !exist);
		
		return tips;
	}
	
	/**
	 * 根据tableId查询出列
	 * @param tables
	 * @param filter
	 * @return
	 */
	@ApiOperation(value = "根据tableId查询出列")
	@PostMapping(value = "/getAllColumns")
	public List<MetaColumnDto> getAllColumns(@RequestBody String tables, String filter) {
        JSONObject jsonObject = JSONObject.parseObject(tables);
	    String jsonTable = jsonObject.getString("tables");
		filter = jsonObject.getString("filter");
		return metaColumnService.selectMetaColumnsByTableId(jsonTable, filter);
	}
	
	/**
	 * 获取主表关联字段
	 * @param tableId
	 * @return
	 */
	@RequestMapping(value = "/getRelationColumns")
	@ResponseBody
	public List<MetaColumnDto> getRelationColumns(String tableId) {
		//UserProfile profile = UserProfileUtils.getUserProfile();
		String staffCode = Context.getIdentityContext().getIdentity().getId();
		return metaColumnService.selectRelationTablesByTableId(tableId, staffCode);
	}
	
	/**
	 * 根据tableId查询metaColumnDTO
	 * @param tableId tableId
	 * @return
	 */
	@ApiOperation(value = "根据tableId查询metaColumnDTO")
	@GetMapping(value = "/selectMetaColumnDto")
	public Map<String, Object> selectMetaColumnDto(String tableId){
		Map<String, Object> result = new HashMap<String, Object>();

		result.put("rows", metaColumnService.selectMetaColumnDtoByTableId(tableId));
		return result;
	}

	
	/**
	 * 同步列信息
	 * @return
	 */
	@ApiOperation(value = "同步列信息")
	@PostMapping(value = "/updateMetaColumn")
	public Map<String, Object> updateMetaColumn(@RequestBody JSONObject params){
		String metaColumnList = params.getString("mcList");
		String tableId = params.getString("tableId");
		Map<String, Object> tips = new HashMap<String, Object>();
		/*同步元数据列信息 */
		metaColumnService.updateMetaColumn(metaColumnList, tableId);
		tips.put(WebCode.SUCCESS, true);
		tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		return tips;
	}
	
	/**
	 * 同步元数据列信息
	 * @param tableId 元数据表ID
	 * @return
	 */
	@ApiOperation(value = "同步元数列信息")
	@GetMapping(value = "/update")
	public Map<String, Object> updateColumn(String tableId) {
		Map<String, Object> tips = new HashMap<String, Object>();
		metaColumnService.updateMetaColumnByTableId(tableId);
		tips.put(WebCode.SUCCESS, true);
		tips.put(WebCode.MSG, WebCode.SUCCESS_OPERATE);
		return tips;
	}


	/**
	 * 根据tableId查询metaColumnDTO
	 * @param tableId tableId
	 * @return
	 */
	@ApiOperation(value = "根据tableId查询metaColumn")
	@GetMapping(value = "/selectMetaColumn")
	public Map<String, Object> selectMetaColumn(String tableId){
		Map<String, Object> result = new HashMap<String, Object>();
		List<Filter> filters = new Filter.Builder().eq("tableId", tableId).build();
		result.put("rows", metaColumnService.select(filters));
		return result;
	}


}